package main.common.strategy.impl;

import main.common.strategy.DiscountStrategy;

public class TenYuanOffForHundredDiscountStrategy implements DiscountStrategy {

    //单例
    private static volatile TenYuanOffForHundredDiscountStrategy instance;

    private TenYuanOffForHundredDiscountStrategy() {
        //防止反射调用构造器
        if(instance != null) {
            throw new RuntimeException("单例模式失败");
        }
    }

    public static TenYuanOffForHundredDiscountStrategy getInstance() {
        if(instance == null) {
            synchronized(TenYuanOffForHundredDiscountStrategy.class) {
                if(instance == null) {
                    instance = new TenYuanOffForHundredDiscountStrategy();
                }
            }
        }
        return instance;
    }


    // 防止反序列化重新创建对象
    private TenYuanOffForHundredDiscountStrategy readResolve() {
        return getInstance();
    }

    @Override
    public double applyDiscount(double total) {
        if(total < 100) {
            return total;
        }
        return total - 10;

    }
}
